﻿using System.Reflection;
using ConsoleApp1.utils;
using WinFormsApp1.bo;
using WinFormsApp1.outputBo;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;


namespace WinFormsApp1.utils;

public class ExcelProcess
{
    /**
     * 读取第一个excel中的三个sheet, 获得三个List
     */
    public static void readInputExcel1(ref List<TPSTR04> TPSTR04List, ref List<TPMOUTR02> TPMOUTR02List,
        ref List<TMMTR00> TMMTR00List)
    {
        IWorkbook workBook = null;
        try
        {
            workBook = new XSSFWorkbook(new FileInputStream(File.OpenRead(GlobalParameters.getExcelLoc(0))));
        }
        catch (Exception e)
        {
            LogUtil.addLog("Excel文件1读取错误");
            throw;
        }

        try
        {
            ISheet sheet0 = workBook.GetSheetAt(0);
            Converter.fromSheetToTPSTR04List(sheet0, ref TPSTR04List);
            ISheet sheet1 = workBook.GetSheetAt(1);
            Converter.fromSheetToTPMOUTR02List(sheet1, ref TPMOUTR02List);
            ISheet sheet2 = workBook.GetSheetAt(2);
            Converter.fromSheetToTMMTR00List(sheet2, ref TMMTR00List);
        }
        catch (Exception e)
        {
            LogUtil.addLog("Excel文件1解析错误");
            throw;
        }
    }

    /**
     * 读取第二个excel中的一个sheet, 获得一个List
     */
    public static void readInputExcel2(ref List<ActualCutResult> actualCutResultList)
    {
        IWorkbook workBook = null;
        try
        {
            workBook = new HSSFWorkbook(new FileInputStream(File.OpenRead(GlobalParameters.getExcelLoc(1))));
        }
        catch (Exception e)
        {
            LogUtil.addLog("Excel文件2读取错误");
            throw;
        }

        try
        {
            ISheet sheet0 = workBook.GetSheetAt(0);
            Converter.fromSheetToActualCutResultList(sheet0, ref actualCutResultList);
        }
        catch (Exception e)
        {
            LogUtil.addLog("Excel文件2解析错误");
            throw;
        }
    }


    /**
     * 将三个List分别写入一个Excel的三个sheet中, 输出至文件中
     */
    public static void writeOutputToExcel(List<RateOfRecord> rateOfRecordList, List<RateOfPlan> rateOfPlanList,
        List<RateOfAll> rateOfAllList)
    {
        IWorkbook workbook = new HSSFWorkbook();
        try
        {
            ISheet sheet0 = workbook.CreateSheet("sheet0");
            Converter.fromRateOfRecordListToSheet(ref sheet0, rateOfRecordList);
            ISheet sheet1 = workbook.CreateSheet("sheet1");
            Converter.fromRateOfPlanListListToSheet(ref sheet1, rateOfPlanList);
            ISheet sheet2 = workbook.CreateSheet("sheet2");
            Converter.fromRateOfAllListToSheet(ref sheet2, rateOfAllList);
        }
        catch (Exception e)
        {
            LogUtil.addLog("通过计算结果生成Excel错误");
            throw;
        }

        try
        {
            FileStream fsWrite = File.OpenWrite(GlobalParameters.getExcelLoc(2));
            workbook.Write(fsWrite);
            fsWrite.Close();
        }
        catch (Exception e)
        {
            LogUtil.addLog("计算结果写入目标文件错误");
            throw;
        }
    }
}